package com.weipeep.demo;

import com.google.common.collect.Lists;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.property.*;
import com.weipeep.demo.entity.ExportClass;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;

public class ExportClassServiceImpl {
    public static void generatePdf(List<ExportClass> exportClassList) throws IOException {
        PdfFont pdfFont = null;
        PdfWriter writer = null;
        try {
            pdfFont = PdfFontFactory.createFont("Songti.ttc,1", PdfEncodings.IDENTITY_H, false);
            writer = new PdfWriter("demo.pdf");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        PdfDocument pdfDoc = new PdfDocument(writer);
        TextFooterEventHandler textFooterEventHandler = new TextFooterEventHandler(pdfFont,"YTT452","2018-01-09",1);
        pdfDoc.addEventHandler(PdfDocumentEvent.INSERT_PAGE,textFooterEventHandler);
        Document doc = new Document(pdfDoc,PageSize.A4,false);//构建文档对象
        doc = ttt(doc,pdfFont);
        pdfDoc.removeAllHandlers();
        TextFooterEventHandler textFooterEventHandlerNew = new TextFooterEventHandler(pdfFont,"UY67892","2018-02-12",1);
        pdfDoc.addEventHandler(PdfDocumentEvent.INSERT_PAGE,textFooterEventHandlerNew);
        doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
        doc = ttt(doc,pdfFont);
        pdfDoc.removeAllHandlers();
        TextFooterEventHandler textFooterEventHandlerNew1 = new TextFooterEventHandler(pdfFont,"TT22311","2018-05-12",1);
        pdfDoc.addEventHandler(PdfDocumentEvent.INSERT_PAGE,textFooterEventHandlerNew1);
        doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
        doc = ttt(doc,pdfFont);
        pdfDoc.removeAllHandlers();
        TextFooterEventHandler textFooterEventHandlerNew2 = new TextFooterEventHandler(pdfFont,"YD609224","2018-06-12",1);
        pdfDoc.addEventHandler(PdfDocumentEvent.INSERT_PAGE,textFooterEventHandlerNew2);
        doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
        doc = ttt(doc,pdfFont);
        pdfDoc.removeAllHandlers();
        PageFooterEventHandler pageFooterEventHandler = new PageFooterEventHandler(pdfFont,pdfDoc.getNumberOfPages());
        pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE,pageFooterEventHandler);
        doc.flush();
        doc.close();



    }
    public static List<ExportClass>  exportClasses(){
        List<ExportClass> exportClassList = Lists.newArrayList();
        for (int i=0;i<10;i++){
            for (int j=0;j<32;i++){

            }
        }
        return exportClassList;
    }
    public static void main(String[] args) {
        try {
            generatePdf(null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Document ttt(Document document,PdfFont pdfFont1) throws MalformedURLException {
        Div imageDiv = new Div();
        Div schoolDiv = new Div().setTextAlignment(TextAlignment.CENTER);
        Div courseDiv = new Div().setTextAlignment(TextAlignment.CENTER);
        Div lessonDiv = new Div().setTextAlignment(TextAlignment.CENTER);
        Div beizhuDiv = new Div().setTextAlignment(TextAlignment.CENTER);
        Div tipDiv = new Div().setTextAlignment(TextAlignment.LEFT);
        imageDiv.setMarginLeft(50);
        imageDiv.setMarginTop(20);
        Image image = new Image(ImageDataFactory.create("logo.png"));
        image.setHeight(25);
        imageDiv.add(image);
        UnitValue[] courseTableWidth = new UnitValue[]{new UnitValue(2,25),new UnitValue(2,55)};
        UnitValue[] beizhuTableWidth =new UnitValue[]{new UnitValue(2,40),new UnitValue(2,40)};
        UnitValue[] lessonTableWidth = new UnitValue[]{new UnitValue(2,25),new UnitValue(2,25),new UnitValue(2,25),new UnitValue(2,25)};
        UnitValue[] tipWidth = new UnitValue[]{new UnitValue(2,100)};

        Table courseTable = new Table(courseTableWidth).setFont(pdfFont1).setFontSize(10).setWidth(new UnitValue(2,90)).setHorizontalAlignment(HorizontalAlignment.CENTER);
        courseTable.addCell(new Cell().add(new Paragraph("上课日期:").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));
        courseTable.addCell(new Cell().add(new Paragraph("2017年11月4日 至 2017年12月24日").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));

        courseTable.addCell(new Cell().add(new Paragraph("上课地点:").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));
        courseTable.addCell(new Cell().add(new Paragraph("朝阳国贸校区中航工业大厦214教室(与YA17725合班)").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));

        courseTable.addCell(new Cell().add(new Paragraph("节、假日:").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));
        courseTable.addCell(new Cell().add(new Paragraph("--------").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));

        courseTable.addCell(new Cell().add(new Paragraph("上课时间:").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));
        courseTable.addCell(new Cell().add(new Paragraph("每周六、日13:30-15:30,15:40-17:40").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));

        courseTable.addCell(new Cell().add(new Paragraph("课程设置:").setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));
        courseTable.addCell(new Cell().add(new Paragraph("IELTS基础写作(8) IELTS基础听力(8)\n" +
                "IELTS基础口语(8) IELTS基础阅读(8)")
                .setTextAlignment(TextAlignment.LEFT)).setBorder(Border.NO_BORDER));
        schoolDiv.add(new Paragraph("北京新东方学校\nIELTS5.5分班(25人)(YA17747)课程表\n").setFont(pdfFont1).setFontSize(15));
        courseDiv.add(courseTable);

        Table beizhuTable = new Table(beizhuTableWidth).setFont(pdfFont1).setFontSize(10).setWidth(new UnitValue(2,90)).setHorizontalAlignment(HorizontalAlignment.CENTER);
        beizhuTable.addCell(new Cell().setBorder(Border.NO_BORDER).add(new Paragraph("备注：").setFont(pdfFont1).setFontSize(10).setTextAlignment(TextAlignment.LEFT)));
        beizhuTable.addCell(new Cell().setBorder(Border.NO_BORDER).add(new Paragraph("共计25").setFont(pdfFont1).setFontSize(10).setTextAlignment(TextAlignment.RIGHT)));
        beizhuDiv.add(beizhuTable);
        Table lessonTable = new Table(lessonTableWidth).setFont(pdfFont1).setFontSize(10).setWidth(new UnitValue(2,90)).setHorizontalAlignment(HorizontalAlignment.CENTER);
        for (int i=1;i<100;i++){
            Cell cell = new Cell().setKeepTogether(true).setPadding(0)
                    .add(new Paragraph("2017年11月4日 星期六").setBackgroundColor(ColorConstants.GRAY))
                    .add(new Div().setMargin(0).setWidth(new UnitValue(2,100)).setBorderBottom(new SolidBorder(ColorConstants.BLACK,1))
                            .add(new Paragraph("7:30-8:30\n " +
                                    "李老师（YH00223）\n" +
                                    "IELTS基础写作\n" +
                                    "朝阳国贸校区中航工业大厦214教室").setFontSize(8)))
                    .add(new Div().setMargin(0).setWidth(new UnitValue(2,100)).setBorderBottom(new SolidBorder(ColorConstants.BLACK,1))
                            .add(new Paragraph("11:30-13:30\n " +
                                    "张老师（HU02223）\n" +
                                    "IELTS基础写作\n" +
                                    "朝阳国贸校区中航工业大厦214教室").setFontSize(8)));
            if (i%4==0) {
                cell.add(new Div().setMargin(0).setWidth(new UnitValue(2,100)).setBorderBottom(new SolidBorder(ColorConstants.BLACK,1))
                                .add(new Paragraph("17:30-18:30\n " +
                                        "王老师（YG898547）\n" +
                                        "IELTS基础写作\n" +
                                        "朝阳国贸校区中航工业大厦214教室").setFontSize(8)));
                cell.add(new Div().setMargin(0).setWidth(new UnitValue(2,100)).setBorderBottom(new SolidBorder(ColorConstants.BLACK,1))
                                .add(new Paragraph("19:30-20:30\n " +
                                        "陆涛（LT859847）\n" +
                                        "IELTS基础写作\n" +
                                        "朝阳国贸校区中航工业大厦214教室").setFontSize(8)));
            }
            lessonTable.addCell(cell);
        }
        lessonDiv.add(lessonTable);
        Table tipTable = new Table(tipWidth).setFont(pdfFont1).setFontSize(10).setWidth(new UnitValue(2,90)).setHorizontalAlignment(HorizontalAlignment.CENTER);
        tipDiv.add(tipTable.addCell(new Cell().setBorder(Border.NO_BORDER).add(new Paragraph("\n新东方核心价值观：诚信负责、真情关爱、好学精进、志高行远！").setFont(pdfFont1).setBold().setFontSize(8))));
        document.add(imageDiv);
        document.add(schoolDiv);
        document.add(courseDiv);
        document.add(beizhuDiv);
        document.add(lessonDiv);
        document.add(tipDiv);
        return document;
    }
}
